package com.psl.services;

import com.psl.Dao.Conn;
import com.psl.model.product;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class search
{
  public List<product> searchlist(String p_name)
  {
    Connection conn = new Conn().getConn();
    ResultSet rs = null;
    PreparedStatement pstmt = null;
    List product = new ArrayList();
    try
    {
      pstmt = conn.prepareStatement("SELECT p_no,p_name,p_r,p_n,p_uw,p_up,p_kc,p_size from product WHERE p_name LIKE '%" + p_name + "%'");
      rs = pstmt.executeQuery();
      while (rs.next())
      {
        product p = new product();
        p.setP_no(rs.getString(1));
        p.setP_name(rs.getString(2));
        p.setP_r(rs.getString(3));
        p.setP_n(rs.getString(4));
        p.setP_uw(rs.getString(5));
        p.setP_up(rs.getString(6));
        p.setP_kc(rs.getString(7));
        p.setP_size(rs.getString(8));
        product.add(p);
      }
      return product;
    }
    catch (SQLException e)
    {
      e.printStackTrace();
      return null;
    }
    finally
    {
      try
      {
        if (pstmt != null) {
          pstmt.close();
        }
        if (rs != null) {
          rs.close();
        }
        if (conn != null) {
          conn.close();
        }
      }
      catch (SQLException e)
      {
        e.printStackTrace();
      }
    }
  }
}
